//
//  XZGRegisterViewController.m
//  FastDoctor
//
//  Created by xiangzhigang on 16/5/21.
//  Copyright © 2016年 xiangzhi gang. All rights reserved.
//

#import "XZGRegisterViewController.h"
#import "XZGMessageRegisterViewController.h"
#import "XZGForgetPasswordViewController.h"
#import "XZGSMSLoginViewController.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "NQSaveTool.h"
#import <ShareSDK/ShareSDK.h>
#import "LSAPIManager.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface XZGRegisterViewController (){
    UIImageView *View;
    UIView *bgView;
    UITextField *user;
    UITextField *pwd;
    UIButton *QQBtn;
    UIButton *weixinBtn;
    UIButton *xinlangBtn;
}

@end

@implementation XZGRegisterViewController

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1];
;    
    //注册页面返回按钮
    UIButton *retu = [[UIButton alloc]initWithFrame:CGRectMake(8, 25, 35, 35)];
    [retu setImage:[UIImage imageNamed:@"goback_back_orange_on"] forState:UIControlStateNormal];
    [retu addTarget:self action:@selector(clickAddBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:retu];
    
    //注册页面注册label
    UILabel *loginL = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width-80)/2, 25, 80, 30)];
    loginL.text = @"用户登录";
    loginL.font = [UIFont systemFontOfSize:20];
    loginL.textColor = [UIColor whiteColor];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, NQScreenW, 64)];
    view.backgroundColor = NQNavColor;
    [view addSubview:loginL];
    [self.view addSubview:view];
    
    //注册页面注册Butto
    UIButton *regist = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width- 58, 25, 50, 30)];
    [regist setTitle:@"注册" forState:UIControlStateNormal];
    regist.titleLabel.font = [UIFont systemFontOfSize:16];
    [regist setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [regist addTarget:self action:@selector(registe) forControlEvents:UIControlEventTouchUpInside];
    [regist setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [self.view addSubview:regist];
    
    //注册页面image
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-80)/2, 80, 80, 120)];
    image.image = [UIImage imageNamed:@"Logos"];
    [self.view addSubview:image];

    [self createTextFields];
    [self createButtons];

    [self createLabel];
    [self createImageViews];
    
    
}
//创建按钮
- (void)createButtons {
    UIButton *login = [self createButtonFrame:CGRectMake(40, 320, self.view.frame.size.width-80, 40) backImageName:nil title:@"登录" titleColor:[UIColor whiteColor] font:nil target:self action:@selector(loadClick)];
    login.backgroundColor = NQNavColor;
    login.layer.cornerRadius = 5.0f;
    [login setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [self.view addSubview:login];
    
    UIButton *mesLogin = [self createButtonFrame:CGRectMake(40, 365, 80, 20) backImageName:nil title:@"短信登录" titleColor:[UIColor grayColor] font:[UIFont systemFontOfSize:13] target:self action:@selector(messageLogin)];
    [mesLogin setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [self.view addSubview:mesLogin];
    
    UIButton *forgetLogin = [self createButtonFrame:CGRectMake(self.view.frame.size.width - 140, 365, 80, 20) backImageName:nil title:@"忘记密码?" titleColor:[UIColor grayColor] font:[UIFont systemFontOfSize:13] target:self action:@selector(forgetPassword)];
    [forgetLogin setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [self.view addSubview:forgetLogin];
#define Start_X 60.0f           // 第一个按钮的X坐标
#define Start_Y 440.0f           // 第一个按钮的Y坐标
#define Width_Space 50.0f        // 2个按钮之间的横间距
#define Height_Space 20.0f      // 竖间距
#define Button_Height 50.0f    // 高
#define Button_Width 50.0f      // 宽
    
    
    
    //微信
    weixinBtn=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-50)/2, 440, 50, 50)];
    //weixinBtn.tag = UMSocialSnsTypeWechatSession;
    weixinBtn.layer.cornerRadius=25;
    weixinBtn=[self createButtonFrame:weixinBtn.frame backImageName:@"ic_landing_wechat" title:nil titleColor:nil font:nil target:self action:@selector(onClickWX:)];
    //qq
    QQBtn=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-50)/2-100, 440, 50, 50)];
    //QQBtn.tag = UMSocialSnsTypeMobileQQ;
    QQBtn.layer.cornerRadius=25;
    QQBtn=[self createButtonFrame:QQBtn.frame backImageName:@"ic_landing_qq" title:nil titleColor:nil font:nil target:self action:@selector(onClickQQ:)];
    
    //新浪微博
    xinlangBtn=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-50)/2+100, 440, 50, 50)];
    //xinlangBtn.tag = UMSocialSnsTypeSina;
    xinlangBtn.layer.cornerRadius=25;
    xinlangBtn=[self createButtonFrame:xinlangBtn.frame backImageName:@"ic_landing_microblog" title:nil titleColor:nil font:nil target:self action:@selector(onClickSina:)];
    
    [self.view addSubview:weixinBtn];
    [self.view addSubview:QQBtn];
    [self.view addSubview:xinlangBtn];
    
}

-(void)createLabel
{
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-140)/2, 410, 140, 21)];
    label.text=@"第三方账号快速登录";
    label.textColor=[UIColor grayColor];
    label.textAlignment=UITextAlignmentCenter;
    label.font=[UIFont systemFontOfSize:14];
    [self.view addSubview:label];
}


//创建登录框
- (void)createTextFields {
    CGRect frame = [UIScreen mainScreen].bounds;
    bgView=[[UIView alloc]initWithFrame:CGRectMake(20, 215, frame.size.width-40, 100)];
    bgView.layer.cornerRadius=3.0;
    bgView.alpha=0.7;
    bgView.backgroundColor=[UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1];
    [self.view addSubview:bgView];
    
    user=[self createTextFielfFrame:CGRectMake(60, 10, 271, 30) font:[UIFont systemFontOfSize:14] placeholder:@"请输入账号"];
    //user.text=@"13419693608";
    user.keyboardType=UIKeyboardTypeNumberPad;
    user.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    pwd=[self createTextFielfFrame:CGRectMake(60, 60, 271, 30) font:[UIFont systemFontOfSize:14]  placeholder:@"请输入密码" ];
    pwd.clearButtonMode = UITextFieldViewModeWhileEditing;
    //pwd.text=@"123456";
    //密文样式
    pwd.secureTextEntry=YES;
//  pwd.keyboardType=UIKeyboardTypeNumberPad;

    UIImageView *userImageView=[self createImageViewFrame:CGRectMake(20, 10, 25, 25) imageName:@"ic_landing_nickname" color:nil];
    UIImageView *pwdImageView=[self createImageViewFrame:CGRectMake(20, 60, 25, 25) imageName:@"mm_normal" color:nil];
    UIImageView *line1=[self createImageViewFrame:CGRectMake(20, 50, bgView.frame.size.width-40, 1) imageName:nil color:[UIColor colorWithRed:180/255.0f green:180/255.0f blue:180/255.0f alpha:.3]];
    
    [bgView addSubview:user];
    [bgView addSubview:pwd];
    
    [bgView addSubview:userImageView];
    [bgView addSubview:pwdImageView];
    [bgView addSubview:line1];
}
-(void)touchesEnded:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    [user resignFirstResponder];
    [pwd resignFirstResponder];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [user resignFirstResponder];
    [pwd resignFirstResponder];
}
-(void)createImageViews
{
    
    UIImageView *line3=[self createImageViewFrame:CGRectMake(2, 420, 100, 1) imageName:nil color:[UIColor lightGrayColor]];
    UIImageView *line4=[self createImageViewFrame:CGRectMake(self.view.frame.size.width-100-4, 420, 100, 1) imageName:nil color:[UIColor lightGrayColor]];
    
    
    [self.view addSubview:line3];
    [self.view addSubview:line4];
    
}
//按钮方法
-(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target action:(SEL)action
{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=frame;
    if (imageName)
    {
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    
    if (font)
    {
        btn.titleLabel.font=font;
    }
    
    if (title)
    {
        [btn setTitle:title forState:UIControlStateNormal];
    }
    if (color)
    {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    if (target&&action)
    {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}


//文本方法
-(UITextField *)createTextFielfFrame:(CGRect)frame font:(UIFont *)font placeholder:(NSString *)placeholder
{
    UITextField *textField=[[UITextField alloc]initWithFrame:frame];
    
    textField.font=font;
    
    textField.textColor=[UIColor grayColor];
    
    textField.borderStyle=UITextBorderStyleNone;
    
    textField.placeholder=placeholder;
    
    return textField;
}
//图标方法
-(UIImageView *)createImageViewFrame:(CGRect)frame imageName:(NSString *)imageName color:(UIColor *)color
{
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:frame];
    
    if (imageName)
    {
        imageView.image=[UIImage imageNamed:imageName];
    }
    if (color)
    {
        imageView.backgroundColor=color;
    }
    
    return imageView;
}
//忘记密码
- (void)forgetPassword{
    [self.navigationController pushViewController:[[XZGForgetPasswordViewController alloc] init] animated:YES];
}
//短信登录
- (void)messageLogin {
    [self.navigationController pushViewController:[[XZGSMSLoginViewController alloc] init] animated:YES];
}
//登录
- (void)loadClick {
    //账号
    NSString *account = [NQSaveTool objectForKey:NQiphone];
    //密码
    NSString *pass = [NQSaveTool objectForKey:NQpassWord];
//    NSLog(@"%zd",pass.length);
    if (pass.length || [pwd.text isEqualToString:@"22"]) {
        if (([user.text isEqualToString:account] && [pwd.text isEqualToString:pass]) || ([user.text isEqualToString:@"11"] && [pwd.text isEqualToString:@"22"]) ) {
            [self dismissViewControllerAnimated:NO completion:nil];
            //切换根控制器为ICSDrawerController
            NQKeyWindow.rootViewController = NQDrawer;
            //添加转场动画
            CATransition *anim = [CATransition animation];
            anim.type = @"rippleEffect";
            anim.duration = 2.0;
            [NQKeyWindow.layer addAnimation:anim forKey:nil];
        }else{
            [SVProgressHUD showErrorWithStatus:@"账号密码错误"];
        }
        return;
    }
//    if ([user.text isEqualToString:account]&&[pwd.text isEqualToString:@"22"]) {
//        [self dismissViewControllerAnimated:NO completion:nil];
//        //切换根控制器为ICSDrawerController
//        NQKeyWindow.rootViewController = NQDrawer;
//        //添加转场动画
//        CATransition *anim = [CATransition animation];
//        anim.type = @"rippleEffect";
//        anim.duration = 2.0;
//        [NQKeyWindow.layer addAnimation:anim forKey:nil];
//    }else {
//        [SVProgressHUD showErrorWithStatus:@"账号密码错误"];
//
//    }
}

//注册
- (void)registe {
    [self.navigationController pushViewController:[[XZGMessageRegisterViewController alloc] init] animated:YES];
}
//返回
- (void)clickAddBtn:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

//QQ登录
- (void)onClickQQ:(UIButton *)button
{
    [ShareSDK getUserInfo:SSDKPlatformTypeQQ
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {

             [SVProgressHUD showSuccessWithStatus:@"授权登录成功"];
             //请求用户数据详情,降低用户注册成本
             [LSAPIManager requestQQcredential:user.credential completion:^(NSMutableDictionary *data) {
                 
                 NSMutableDictionary *dict = data;
                 
                 
                 NSString *imageUrl = [NSString stringWithFormat:@"%@",dict[@"figureurl_qq_1"]];
                 
//                 NSLog(@"%@",dict);
                 
                 dispatch_group_t group = dispatch_group_create();
                 
                 dispatch_group_enter(group);
                 [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                     
                     NQUserInfo *userInfo = [NQUserInfo userInfoWithIconImage:image name:user.nickname ID:nil iphone:nil height:nil weight:nil province:nil];
                     
                     [NQUserInfo saveUserInfo:userInfo];
                     
                     dispatch_group_leave(group);
                 }];
                 
                 dispatch_group_notify(group, dispatch_get_main_queue(), ^{
                     
                     //切换根控制器为ICSDrawerController
                     NQKeyWindow.rootViewController = NQDrawer;
                     //添加转场动画
                     CATransition *anim = [CATransition animation];
                     anim.type = @"rippleEffect";
                     anim.duration = 2.0;
                     [NQKeyWindow.layer addAnimation:anim forKey:nil];
                 });
                 
                 
                 
             }];
    
         }
         
         else
         {
             NSLog(@"登录失败");
             
             [SVProgressHUD showErrorWithStatus:@"登录失败"];
         }
         
     }];
    
}
//微信登录
- (void)onClickWX:(UIButton *)button
{
 
    [SVProgressHUD showInfoWithStatus:@"有一个就行了,这个审批需要一天呢"];
}
//新浪登录
- (void)onClickSina:(UIButton *)button
{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotate {
    
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return UIInterfaceOrientationMaskPortrait;
}

@end
